*     *****************************************************************    *;
*     *****************************************************************    *;
*   Date:        June 2021   		                                       *;
*   Authors:     Dellmuth, Scholte, Tallberg, and Verhaegen                *;
*   Purpose:     Make figures using dyadic data						       *;
*     *****************************************************************    *;
*     *****************************************************************    *;

set more off     
vers 16.1

*note: "_d" denotes variables for citizens

*****************************
*Descriptives section t-tests for text
*****************************

use dyads_recoded.dta, clear

ttest DIFFconfgov=DIFFconfWTO
ttest DIFFconfgov=DIFFconfIMF
*tab country, gen(c)
gen rude=.
replace rude=0 if country==643
replace rude=1 if country==276
gen ruus=.
replace ruus=0 if country==643
replace ruus=1 if country==840
gen deus=.
replace deus=0 if country==276
replace deus=1 if country==840
ttest DIFFconfios , by(rude)
ttest DIFFconfios , by(ruus)
ttest DIFFconfios , by(deus)

*tab elitecat
gen parmed=.
replace parmed=0 if elitecat==1
replace parmed=1 if elitecat==4
ttest DIFFconfios , by(parmed)



*****************************
* Descriptive figures - gaps
*****************************

set scheme plotplain

* Figure 1: by institution // 90-60-20 filling intensity, black, bar label format %9.2f

use "elites_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
sum confios
sum confWTO
sum confUN
sum confIMF
sum confICC
sum confWHO
sum confWB
tab confgov
collapse confios confUN confIMF confICC confWB confWHO confWTO confgov
gen group="Elites"
save fig1_elites.dta, replace

use "citizens_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
sum confWTO
sum confUN
sum confIMF
sum confICC
sum confWHO
sum confWB
tab confgov
gen confios= (confUN +confIMF +confICC +confWB +confWHO +confWTO)/6
collapse confios confUN confIMF confICC confWB confWHO confWTO confgov
gen group="Citizens"
save fig1_citizens.dta, replace

use fig1_citizens.dta, clear
append using fig1_elites.dta
*add diff manually 0.405139	0.55245	0.34656	0.49347	0.41653	0.53504	0.25032	0.3618
replace group="Citizen-elite gap" in 3 
label var confICC "ICC"
label var confIMF "IMF"
label var confios "All IOs"
label var confUN "UN"
label var confWHO "WHO"
label var confWTO "WTO"
label var confWB "WB"
ed
save fig1.dta, replace


use fig1.dta, clear
gen id=_n
reshape long conf, i(id) j(io) string 
ed
replace io="All IOs" if io=="ios"
replace io="Gov." if io=="gov"
encode io, gen(ionum)
encode group, gen(groupnum)
drop io id group
reshape wide conf, i(ionum) j(groupnum)
rename conf1 confcitizens
rename conf2 confgap
rename conf3 confelites
#delimit ;
graph bar confelites confcitizens confgap, 
 over(ionum, sort(confgap) descending label(labcolor(black) labsize(small))) 
 ylabel(0 "None at all" 1 "Not very much" 2 "Quite a lot" 3 "A great deal", 
labsize(small) labcolor(black) angle(horizontal) ///
glcolor(gs14) gmax) bar(3, color(g2)) legend(col(1) size(medsmall)order(1 "Elites" 2 "Citizens" 3 "Elite-citizen gap"))
 ytitle(Confidence)
bar(1, fcolor(gs14) fintensity(100)) 
bar(2, fcolor(gs8) fintensity(100)) 
bar(3, fcolor(gs2) fintensity(100)) 
blabel(bar, format(%9.1f) size(small) lcolor(none))
;
#delimit cr





* Figure 2: by country
*****************************

use "elites_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
collapse confios, by(country)
gen group="Elites"
save fig2_elites.dta, replace

use "citizens_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
gen confios= (confUN +confIMF +confICC +confWB +confWHO +confWTO)/6
collapse confios, by(country)
gen group="Citizens"
save fig2_citizens.dta, replace


use fig2_citizens.dta, clear
append using fig2_elites.dta
gen countrystr=""
replace countrystr="Brazil" if country==76
replace countrystr="Philippines" if country==608
replace countrystr="US" if country==840
replace countrystr="Germany" if country==276
replace countrystr="Russia" if country==643
local new = _N + 8
set obs `new'
replace countrystr="All" in 11/13
replace group="Citizens" in 11
replace group="Elites" in 12
replace group="Elite-citizen gap" in 13
replace confios=1.4 in 11
replace confios=1.8 in 12
replace confios=0.4 in 13
/*add:
0.630962
0.476256
-0.118715
0.505918
0.43243
*/
save fig2.dta, replace

use fig2.dta, clear
encode countrystr, gen(countrynum)
encode group, gen(groupnum)
sort group country 
ed
drop country countrystr group 
reshape wide conf, i(countrynum) j(groupnum)
rename confios1 confcitizens
rename confios2 confgap
rename confios3 confelites
#delimit ;
graph bar confelites confcitizens confgap, 
 over(countrynum, sort(confgap) descending label(labcolor(black) labsize(small))) 
 ylabel(0 "None at all" 1 "Not very much" 2 "Quite a lot" 3 "A great deal", 
labsize(small) labcolor(black) angle(horizontal) ///
glcolor(gs14) gmax) bar(3, color(g2)) legend(col(1) size(medsmall)order(1 "Elites" 2 "Citizens" 3 "Elite-citizen gap"))
 ytitle(Confidence)
bar(1, fcolor(gs14) fintensity(100)) 
bar(2, fcolor(gs8) fintensity(100)) 
bar(3, fcolor(gs2) fintensity(100)) 
blabel(bar, format(%9.1f) size(small) lcolor(none))
;
#delimit cr




* Figure 3 - by elite type
*****************************

use "citizens_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
mean confios // 1.374962

use "elites_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
tab elitecat
collapse confios, by(elitecat)
save fig3_elites.dta, replace
*manually add citizen mean and diff, and bring in wide format

use fig3_elites.dta, clear
ed
gen group="Elites"
local new = _N + 12
set obs `new'
replace group="Citizens" in 7/12
replace confios=1.374962 in 7/12
replace group="Elite-citizen gap" in 13/18
replace confios=0.4 in 13
/*add diff:
0.314554
0.569482
0.174334
0.32044
0.458371
0.477419
*/
label define sectors 1 "Party" 2 "Bureaucratic" 3 "Civil society" 4 "Media" 5 "Business" 6 "Research" 7 "All" 8 "", modify
label values elitecat sectors
*add new obs for elitecat manually
*add three "7" for "All"

gen countrystr=""
replace countrystr="Brazil" if country==76
replace countrystr="Philippines" if country==608
replace countrystr="US" if country==840
replace countrystr="Germany" if country==276
replace countrystr="Russia" if country==643
local new = _N + 8
set obs `new'
replace group="Citizens" in 19
replace group="Elites" in 20
replace group="Elite-citizen gap" in 21
replace confios=1.4 in 19
replace confios=1.8 in 20
replace confios=0.4 in 21
save fig3.dta, replace

use fig3.dta, clear 
encode group, gen(groupnum)
sort group elitecat 
ed
drop group 
reshape wide conf, i(elitecat) j(groupnum)
rename confios1 confcitizens
rename confios2 confgap
rename confios3 confelites
#delimit ;
graph bar confelites confcitizens confgap, 
 over(elitecat, sort(confgap) descending label(labcolor(black) labsize(2))) 
 ylabel(0 "None at all" 1 "Not very much" 2 "Quite a lot" 3 "A great deal", 
labsize(small)  labcolor(black) angle(horizontal) ///
glcolor(gs14) gmax) bar(3, color(g2)) 
legend(col(1) size(medsmall) order(1 "Elites" 2 "Citizens" 3 "Elite-citizen gap"))
 ytitle(Confidence)
bar(1, fcolor(gs14) fintensity(100)) 
bar(2, fcolor(gs8) fintensity(100)) 
bar(3, fcolor(gs2) fintensity(100)) 
blabel(bar, format(%9.1f) size(small) lcolor(none))
;
#delimit cr




*Statistical significance of these differences? - info for figure notes
*fig1
use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
tab resptype
ttest confICC, by(resptype)
ttest confIMF, by(resptype)
ttest confUN , by(resptype)
ttest confWB, by(resptype)
ttest confWHO, by(resptype)
ttest confWTO, by(resptype)
ttest confios , by(resptype)
*fig2
tab resptype
ttest confios if country==76, by(resptype)
ttest confios if country==276, by(resptype)
ttest confios if country==608, by(resptype)
ttest confios if country==643, by(resptype)
ttest confios if country==840, by(resptype)
*fig3
use "dyads_recoded.dta", clear
tab elitecat
tab elitecat, nol
ttest confios=confios_d if elitecat==1
ttest confios=confios_d if elitecat==2
ttest confios=confios_d if elitecat==3
ttest confios=confios_d if elitecat==4
ttest confios=confios_d if elitecat==5
ttest confios=confios_d if elitecat==6


